/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.upc.upcdewtaxiremisse.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import pe.edu.upc.upcdewtaxiremisse.model.Usuario;
import pe.edu.upc.upcdewtaxiremisse.service.TaxiService;

/**
 *
 * @author Sergio
 */
public class LoginServlet extends HttpServlet {

    private TaxiService taxiService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
        this.taxiService = (TaxiService) context.getBean("taxiService");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /*PrintWriter writer = resp.getWriter();
        writer.println("Bienvenido");
        writer.close();*/
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Usuario usuario = taxiService.getUsuario(new Usuario(username, password));
        if (usuario != null) {
            System.out.println("Login(Usuario="  + usuario.getIdUsuario() + "). Ingresando ...");
            req.getSession().setAttribute("SessionUsuario", usuario);
            req.getRequestDispatcher("Programacion").forward(req, resp);
        } else {
            req.getRequestDispatcher("ingreso.jsp").forward(req, resp);
        }

    }
}
